package br.com.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "converteData")
public class ConverteData implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        String data = String.valueOf(value);
        if (data != null && !data.equals("")) {
            data = data.replaceAll("\\/", "");
        }
        return data;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String data = String.valueOf(value);
        if (data != null && data.length() == 8) {
            data = data.substring(0, 2) + "/"
                    + data.substring(2, 4) + "/"
                    + data.substring(4, 8);
        }
        return data;
    }
}
